package com.rsk.swagger;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2 接口Api文档 配置文件
 * 
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
	
	@Autowired
	Environment env;
	
	@Value("${swagger.enable}")
    private boolean enableSwagger;

    /**
     * 初始化创建Swagger Api
     */
    @Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				// 详细信息定制
				.apiInfo(apiInfo())
				.select()
				// 指定当前包路径
				.apis(RequestHandlerSelectors.basePackage(env.getProperty("swagger.scan")))
				// 扫描所有 .apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.any())
				.build()
				.enable(enableSwagger)
				.securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
	}

	/**
	 * 添加摘要信息
	 */
	private ApiInfo apiInfo() {
		// 用ApiInfoBuilder进行定制
		return new ApiInfoBuilder()
					.title(env.getProperty("swagger.title"))
			        .description(env.getProperty("swagger.description"))
			        .version(env.getProperty("swagger.version"))
			        .build();
	}
	
	
	 private List<ApiKey> securitySchemes() {
	        List<ApiKey> apiKeyList= new ArrayList();
	        //注意，这里应对应登录token鉴权对应的k-v
	        apiKeyList.add(new ApiKey("x-auth-token", "x-auth-token", "header"));
	        return apiKeyList;
	    }

	    private List<SecurityContext> securityContexts() {
	        List<SecurityContext> securityContexts=new ArrayList();
	        securityContexts.add(
	                SecurityContext.builder()
	                        .securityReferences(defaultAuth())
	                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
	                        .build());
	        return securityContexts;
	    }

	    List<SecurityReference> defaultAuth() {
	        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
	        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
	        authorizationScopes[0] = authorizationScope;
	        List<SecurityReference> securityReferences=new ArrayList();
	        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
	        return securityReferences;
	    }
	    
}

